package com.wei.wiki.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wei.wiki.mapper.CategoryMapper;
import com.wei.wiki.pojo.Category;
import com.wei.wiki.pojo.CategoryExample;
import com.wei.wiki.req.CategoryQueryReq;
import com.wei.wiki.req.CategorySaveReq;
import com.wei.wiki.resp.CategoryQueryResp;
import com.wei.wiki.resp.PageResp;
import com.wei.wiki.service.CategoryService;
import com.wei.wiki.util.CopyUtil;
import com.wei.wiki.util.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/*
 * @author : ShunWei
 * @date : 2022/1/27 20:54
 * @Description:
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    private CategoryMapper categoryMapper;

    @Autowired
    public void setCategoryMapper(CategoryMapper categoryMapper) {
        this.categoryMapper = categoryMapper;
    }

    @Resource
    private SnowFlake snowFlake;

    @Override
    public PageResp<CategoryQueryResp> categoryList(CategoryQueryReq req) {
        CategoryExample example = new CategoryExample();
        example.setOrderByClause("sort asc");
        CategoryExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(true);
        PageHelper.startPage(req.getPage(), req.getSize());
        List<Category> categoryList = categoryMapper.selectByExample(example);
        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);

//        List<CategoryQueryResp> respList = new ArrayList<>();
//        for (Category category : categoryList) {
////            CategoryQueryResp categoryResp = new CategoryQueryResp();
////            BeanUtils.copyProperties(category, categoryResp);
//            CategoryQueryResp categoryResp = CopyUtil.copy(category, CategoryQueryResp.class);
//            respList.add(categoryResp);
//        }
        List<CategoryQueryResp> list = CopyUtil.copyList(categoryList, CategoryQueryResp.class);
        PageResp<CategoryQueryResp> pageResp = new PageResp();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    @Override
    public List<CategoryQueryResp> all() {
        CategoryExample example = new CategoryExample();
        example.setOrderByClause("sort asc");
        CategoryExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(true);
        List<Category> categoryList = categoryMapper.selectByExample(example);
        List<CategoryQueryResp> list = CopyUtil.copyList(categoryList, CategoryQueryResp.class);

        return list;
    }

    @Override
    public void save(CategorySaveReq req) {
        Category category = CopyUtil.copy(req, Category.class);
        if (ObjectUtils.isEmpty(category.getId())) {
            // 新增
            category.setId(snowFlake.nextId());
            category.setCreateTime(new Date());
            category.setStatus(true);
            categoryMapper.insert(category);
        } else {
            // 更新
            category.setUpdateTime(new Date());
            category.setStatus(true);
            categoryMapper.updateByPrimaryKey(category);
        }
    }

    @Override
    public void delete(Long id) {
        categoryMapper.deleteByPrimaryKey(id);
    }
}
